package com.ysd.lis.service.interfacePlatform.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysBasDictDetail;
import com.ysd.lis.entity.SysModule;
import com.ysd.lis.entity.interfacePlatform.SysInterfaceFunction;
import com.ysd.lis.entity.interfacePlatform.SysInterfaceFunctions;
import com.ysd.lis.entity.lab.LabItemDict;
import com.ysd.lis.mapper.interfacePlatform.SysInterfaceFunctionMapper;
import com.ysd.lis.request.InterfaceFunctionDto;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.SysModuleService;
import com.ysd.lis.service.interfacePlatform.SysInterfaceFunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 52771
 * @description 针对表【SYS_INTERFACE_SYS_TREE(基础字典分类)】的数据库操作Service实现
 * @createDate 2024-07-15 08:45:13
 */
@Service
public class SysInterfaceFunctionServiceImpl extends ServiceImpl<SysInterfaceFunctionMapper, SysInterfaceFunction>
        implements SysInterfaceFunctionService {


    @Autowired
    SysInterfaceFunctionMapper sysInterfaceFunctionMapper;
    @Autowired
    SysBasDictDetailService sysBasDictDetailService;
    @Autowired
    SysModuleService sysModuleService;


    @Override
    public Result findSysInterfaceFunction(InterfaceFunctionDto pageParam) {
        List<Map> list = new ArrayList<>();
        LambdaQueryWrapper<SysInterfaceFunction> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysInterfaceFunction::getDelFlag, 0);
        queryWrapper.eq(SysInterfaceFunction::getModuleId,pageParam.getModuleId());
        queryWrapper.eq(SysInterfaceFunction::getSystemId,pageParam.getSystemId());
        List<SysInterfaceFunction> dictTypeTree = sysInterfaceFunctionMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(dictTypeTree)){
            List<String> collect = dictTypeTree.stream().map(SysInterfaceFunction::getFunctionId).collect(Collectors.toList());
            List<Map<String, Object>> patTypeList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("InterfaceFunction").getData();
            list = patTypeList.stream().filter(a->collect.contains(a.get("commonId"))).collect(Collectors.toList());
        }
        return Result.succ(1, "查询成功", list);
    }

    @Override
    public Result findSysInterfaceModule() {
        List<SysModule> list = new ArrayList<>();
        LambdaQueryWrapper<SysModule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysModule::getDelFlag, 0);
        List<SysModule> sysModuleList = sysModuleService.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(sysModuleList)){
            List<Map<String, Object>> patTypeList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("InterfaceModule").getData();
            List<String> collect = patTypeList.stream().map(map->map.get("name")+"").collect(Collectors.toList());
            list = sysModuleList.stream().filter(a->collect.contains(a.getName())).collect(Collectors.toList());
        }
        return Result.succ(1, "查询成功", list);
    }


    @Override
    public Result addSysInterfaceFunction(List<SysInterfaceFunction> sysInterfaceFunctions) {
        //先刪除掉之前有的
        if ( CollectionUtils.isNotEmpty(sysInterfaceFunctions)) {
           String moduleId =  sysInterfaceFunctions.get(0).getModuleId();
           String systemId = sysInterfaceFunctions.get(0).getSystemId();
           LambdaQueryWrapper<SysInterfaceFunction> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper
                    .eq(SysInterfaceFunction::getModuleId,moduleId)
                    .eq(SysInterfaceFunction::getSystemId,systemId);
            sysInterfaceFunctionMapper.delete(lambdaQueryWrapper);
        }
        boolean b = this.saveBatch(sysInterfaceFunctions);
        if (b) {
            return Result.succ(1, "添加成功", "");
        } else {
            return Result.succ(400, "添加失败", "");
        }
    }

    @Override
    public Result editSysInterfaceFunction(SysInterfaceFunction sysInterfaceFunction) {
        int updateById = sysInterfaceFunctionMapper.updateById(sysInterfaceFunction);
        if (updateById > 0) {
            return Result.succ(1, "修改成功", sysInterfaceFunction);
        } else {
            return Result.succ(400, "修改失败", "");
        }
    }

    @Override
    public Result deletSysInterfaceFunction(SysInterfaceFunction sysInterfaceFunction) {
        int deleteById = sysInterfaceFunctionMapper.deleteById(sysInterfaceFunction.getId());
        if (deleteById > 0) {
            return Result.succ(1, "删除成功", sysInterfaceFunction);
        } else {
            return Result.fail("删除失败");
        }
    }




}




